home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / man / cat.1 / perlcall.1 < prev    next >
Text File  |  1995-07-25  |  40KB  |  1,057 lines

  1.  
  2.  
  3.  
  4.      PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
  5.  
  6.  
  7.  
  8.      NNNNAAAAMMMMEEEE
  9.           perlcall - Perl calling conventions from C
  10.  
  11.      DDDDEEEESSSSCCCCRRRRIIIIPPPPTTTTIIIIOOOONNNN
  12.           WWWWAAAARRRRNNNNIIIINNNNGGGG :::: TTTThhhhiiiissss ddddooooccccuuuummmmeeeennnntttt iiiissss ssssttttiiiillllllll uuuunnnnddddeeeerrrr ccccoooonnnnssssttttrrrruuuuccccttttiiiioooonnnn.... TTTThhhheeeerrrreeee
  13.           aaaarrrreeee bbbboooouuuunnnndddd ttttoooo bbbbeeee aaaa nnnnuuuummmmbbbbeeeerrrr ooooffff iiiinnnnaaaaccccccccuuuurrrraaaacccciiiieeeessss,,,, ssssoooo ttttrrrreeeeaaaadddd vvvveeeerrrryyyy
  14.           ccccaaaarrrreeeeffffuuuullllllllyyyy ffffoooorrrr nnnnoooowwww....
  15.  
  16.           The purpose of this document is to show you how to write
  17.           _c_a_l_l_b_a_c_k_s, i.e. how to call Perl from C. The main focus is
  18.           on how to interface back to Perl from a bit of C code that
  19.           has itself been run by Perl, i.e. the 'main' program is a
  20.           Perl script; you are using it to execute a section of code
  21.           written in C; that bit of C code wants you to do something
  22.           with a particular event, so you want a Perl sub to be
  23.           executed whenever it happens.
  24.  
  25.           Examples where this is necessary include
  26.  
  27.           o+    You have created an XSUB interface to an application's
  28.                C API.
  29.  
  30.                A fairly common feature in applications is to allow you
  31.                to define a C function that will get called whenever
  32.                something nasty occurs.  What we would like is for a
  33.                Perl sub to be called instead.
  34.  
  35.           o+    The classic example of where callbacks are used is in
  36.                an event driven program like for X-windows.  In this
  37.                case your register functions to be called whenever a
  38.                specific events occur, e.g. a mouse button is pressed.
  39.  
  40.           Although the techniques described are applicable to
  41.           embedding Perl in a C program, this is not the primary goal
  42.           of this document. For details on embedding Perl in C refer
  43.           to the _p_e_r_l_e_m_b_e_d manpage (currently unwritten).
  44.  
  45.           Before you launch yourself head first into the rest of this
  46.           document, it would be a good idea to have read the following
  47.           two documents - the _p_e_r_l_a_p_i manpage and the _p_e_r_l_g_u_t_s
  48.           manpage.
  49.  
  50.           This stuff is easier to explain using examples. But first
  51.           here are a few definitions anyway.
  52.  
  53.           DDDDeeeeffffiiiinnnniiiittttiiiioooonnnnssss
  54.  
  55.           Perl has a number of C functions which allow you to call
  56.           Perl subs. They are
  57.  
  58.  
  59.  
  60.  
  61.  
  62.  
  63.      Page 1                                          (printed 6/30/95)
  64.  
  65.  
  66.  
  67.  
  68.  
  69.  
  70.      PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
  71.  
  72.  
  73.  
  74.               I32 perl_call_sv(SV* sv, I32 flags) ;
  75.               I32 perl_call_pv(char *subname, I32 flags) ;
  76.               I32 perl_call_method(char *methname, I32 flags) ;
  77.               I32 perl_call_argv(char *subname, I32 flags, register char **argv) ;
  78.  
  79.           The key function is _p_e_r_l__c_a_l_l__s_v. All the other functions
  80.           make use of _p_e_r_l__c_a_l_l__s_v to do what they do.
  81.  
  82.           _p_e_r_l__c_a_l_l__s_v takes two parameters, the first is an SV*. This
  83.           allows you to specify the Perl sub to be called either as a
  84.           C string (which has first been converted to an SV) or a
  85.           reference to a sub. Example 7, shows you how you can make
  86.           use of _p_e_r_l__c_a_l_l__s_v.  The second parameter, flags, is a
  87.           general purpose option command. This parameter is common to
  88.           all the _p_e_r_l__c_a_l_l_* functions. It is discussed in the next
  89.           section.
  90.  
  91.           The function, _p_e_r_l__c_a_l_l__p_v, is similar as _p_e_r_l__c_a_l_l__s_v
  92.           except it expects it's first parameter has to be a C char*
  93.           which identifies the Perl sub you want to call, e.g.
  94.           perl_call_pv("fred", 0).
  95.  
  96.           The function _p_e_r_l__c_a_l_l__m_e_t_h_o_d expects its first argument to
  97.           contain a blessed reference to a class. Using that reference
  98.           it looks up and calls methname from that class. See example
  99.           9.
  100.  
  101.           _p_e_r_l__c_a_l_l__a_r_g_v calls the Perl sub specified by the subname
  102.           parameter. It also takes the usual flags parameter. The
  103.           final parameter, argv, consists of a list of C strings to be
  104.           sent to the Perl sub. See example 8.
  105.  
  106.           All the functions return a number. This is a count of the
  107.           number of items returned by the Perl sub on the stack.
  108.  
  109.           As a general rule you should _a_l_w_a_y_s check the return value
  110.           from these functions.  Even if you are only expecting a
  111.           particular number of values to be returned from the Perl
  112.           sub, there is nothing to stop someone from doing something
  113.           unexpected - don't say you havn't been warned.
  114.  
  115.           FFFFllllaaaagggg VVVVaaaalllluuuueeeessss
  116.  
  117.           The flags parameter in all the _p_e_r_l__c_a_l_l_* functions
  118.           consists of any combination of the symbols defined below,
  119.           OR'ed together.
  120.  
  121.           G_SCALAR
  122.                Calls the Perl sub in a scalar context.
  123.  
  124.                Whatever the Perl sub actually returns, we only want a
  125.                scalar. If the perl sub does return a scalar, the
  126.  
  127.  
  128.  
  129.      Page 2                                          (printed 6/30/95)
  130.  
  131.  
  132.  
  133.  
  134.  
  135.  
  136.      PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
  137.  
  138.  
  139.  
  140.                return value from the _p_e_r_l__c_a_l_l_* function will be 1 or
  141.                0. If 1, then the value actually returned by the Perl
  142.                sub will be contained on the top of the stack. If 0,
  143.                then the sub has probably called _d_i_e or you have used
  144.                the G_DISCARD flag.
  145.  
  146.                If the Perl sub returns a list, the _p_e_r_l__c_a_l_l_*
  147.                function will still only return 1 or 0. If 1, then the
  148.                number of elements in the list will be stored on top of
  149.                the stack.  The actual values of the list will not be
  150.                accessable.
  151.  
  152.                G_SCALAR is the default flag setting for all the
  153.                functions.
  154.  
  155.           G_ARRAY
  156.                Calls the Perl sub in a list context.
  157.  
  158.                The return code from the _p_e_r_l__c_a_l_l_* functions will
  159.                indicate how many elements of the stack are used to
  160.                store the array.
  161.  
  162.           G_DISCARD
  163.                If you are not interested in the values returned by the
  164.                Perl sub then setting this flag will make Perl get rid
  165.                of them automatically for you. This will take
  166.                precedence to either G_SCALAR or G_ARRAY.
  167.  
  168.                If you do not set this flag then you may need to
  169.                explicitly get rid of temporary values.  See example 3
  170.                for details.
  171.  
  172.           G_NOARGS
  173.                If you are not passing any parameters to the Perl sub,
  174.                you can save a bit of time by setting this flag. It has
  175.                the effect of of not creating the @_ array for the Perl
  176.                sub.
  177.  
  178.                A point worth noting is that if this flag is specified
  179.                the Perl sub called can still access an @_ array from a
  180.                previous Perl sub. This functionality can be
  181.                illustrated with the perl code below
  182.  
  183.                        sub fred
  184.                          { print "@_\n"  }
  185.  
  186.                        sub joe
  187.                          { &fred }
  188.  
  189.                        &joe(1,2,3) ;
  190.  
  191.                This will print
  192.  
  193.  
  194.  
  195.      Page 3                                          (printed 6/30/95)
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202.      PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
  203.  
  204.  
  205.  
  206.                        1 2 3
  207.  
  208.                What has happened is that fred accesses the @_ array
  209.                which belongs to joe.
  210.  
  211.           G_EVAL
  212.                If the Perl sub you are calling has the ability to
  213.                terminate abnormally, e.g. by calling _d_i_e or by not
  214.                actually existing, and you want to catch this type of
  215.                event, specify this flag setting. It will put an _e_v_a_l {
  216.                } around the sub call.
  217.  
  218.                Whenever control returns from the _p_e_r_l__c_a_l_l_* function
  219.                you need to check the $@ variable as you would in a
  220.                normal Perl script. See example 6 for details of how to
  221.                do this.
  222.  
  223.      EEEEXXXXAAAAMMMMPPPPLLLLEEEESSSS
  224.           Enough of the definition talk, let's have a few examples.
  225.  
  226.           Perl provides many macros to assist in accessing the Perl
  227.           stack. These macros should always be used when interfacing
  228.           to Perl internals.  Hopefully this should make the code less
  229.           vulnerable to changes made to Perl in the future.
  230.  
  231.           Another point worth noting is that in the first series of
  232.           examples I have only made use of the _p_e_r_l__c_a_l_l__p_v function.
  233.           This has only been done to ease you into the topic. Wherever
  234.           possible, if the choice is between using _p_e_r_l__c_a_l_l__p_v and
  235.           _p_e_r_l__c_a_l_l__s_v, I would always try to use _p_e_r_l__c_a_l_l__s_v.
  236.  
  237.           The code for these examples is stored in the file
  238.           _p_e_r_l_c_a_l_l._t_a_r. (Once this document settles down, all the
  239.           example code will be available in the file).
  240.  
  241.           EEEExxxxaaaammmmpppplllleeee1111:::: NNNNoooo PPPPaaaarrrraaaammmmeeeetttteeeerrrrssss,,,, NNNNooootttthhhhiiiinnnngggg rrrreeeettttuuuurrrrnnnneeeedddd
  242.  
  243.           This first trivial example will call a Perl sub, _P_r_i_n_t_U_I_D,
  244.           to print out the UID of the process.
  245.  
  246.               sub PrintUID
  247.               {
  248.                   print "UID is $<\n" ;
  249.               }
  250.  
  251.           and here is the C to call it
  252.  
  253.               void
  254.               call_PrintUID()
  255.               {
  256.                   dSP ;
  257.  
  258.  
  259.  
  260.  
  261.      Page 4                                          (printed 6/30/95)
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.      PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
  269.  
  270.  
  271.  
  272.                   PUSHMARK(sp) ;
  273.                   perl_call_pv("PrintUID", G_DISCARD|G_NOARGS) ;
  274.               }
  275.  
  276.           Simple, eh.
  277.  
  278.           A few points to note about this example.
  279.  
  280.           1.   We aren't passing any parameters to _P_r_i_n_t_U_I_D so
  281.                G_NOARGS can be specified.
  282.  
  283.           2.   Ignore dSP and PUSHMARK(sp) for now. They will be
  284.                discussed in the next example.
  285.  
  286.           3.   We aren't interested in anything returned from
  287.                _P_r_i_n_t_U_I_D, so G_DISCARD is specified. Even if _P_r_i_n_t_U_I_D
  288.                was changed to actually return some _v_a_l_u_e(s), having
  289.                specified G_DISCARD will mean that they will be wiped
  290.                by the time control returns from _p_e_r_l__c_a_l_l__p_v.
  291.  
  292.           4.   Because we specified G_DISCARD, it is not necessary to
  293.                check the value returned from _p_e_r_l__c_a_l_l__s_v. It will
  294.                always be 0.
  295.  
  296.           5.   As _p_e_r_l__c_a_l_l__p_v is being used, the Perl sub is
  297.                specified as a C string.
  298.  
  299.           EEEExxxxaaaammmmpppplllleeee 2222:::: PPPPaaaassssssssiiiinnnngggg PPPPaaaarrrraaaammmmeeeetttteeeerrrrssss
  300.  
  301.           Now let's make a slightly more complex example. This time we
  302.           want to call a Perl sub which will take 2 parameters - a
  303.           string ($s) and an integer ($n). The sub will simply print
  304.           the first $n characters of the string.
  305.  
  306.           So the Perl sub would look like this
  307.  
  308.               sub LeftString
  309.               {
  310.                   my($s, $n) = @_ ;
  311.                   print substr($s, 0, $n), "\n" ;
  312.               }
  313.  
  314.           The C function required to call _L_e_f_t_S_t_r_i_n_g would look like
  315.           this.
  316.  
  317.               static void
  318.               call_LeftString(a, b)
  319.               char * a ;
  320.               int b ;
  321.               {
  322.                   dSP ;
  323.  
  324.  
  325.  
  326.  
  327.      Page 5                                          (printed 6/30/95)
  328.  
  329.  
  330.  
  331.  
  332.  
  333.  
  334.      PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
  335.  
  336.  
  337.  
  338.                   PUSHMARK(sp) ;
  339.                   XPUSHs(sv_2mortal(newSVpv(a, 0)));
  340.                   XPUSHs(sv_2mortal(newSViv(b)));
  341.                   PUTBACK ;
  342.  
  343.                   perl_call_pv("LeftString", G_DISCARD);
  344.               }
  345.  
  346.           Here are a few notes on the C function _c_a_l_l__L_e_f_t_S_t_r_i_n_g.
  347.  
  348.           1.   The only flag specified this time is G_DISCARD. As we
  349.                are passing 2 parameters to the Perl sub this time, we
  350.                have not specified G_NOARGS.
  351.  
  352.           2.   Parameters are passed to the Perl sub using the Perl
  353.                stack.  This is the purpose of the code beginning with
  354.                the line dSP and ending with the line PUTBACK.
  355.  
  356.           3.   If you are going to put something onto the Perl stack,
  357.                you need to know where to put it. This is the purpose
  358.                of the macro dSP - it declares and initialises a local
  359.                copy of the Perl stack pointer.
  360.  
  361.                All the other macros which will be used in this example
  362.                require you to have used this macro.
  363.  
  364.                If you are calling a Perl sub directly from an XSUB
  365.                function, it is not necessary to explicitly use the dSP
  366.                macro - it will be declared for you.
  367.  
  368.           4.   Any parameters to be pushed onto the stack should be
  369.                bracketed by the PUSHMARK and PUTBACK macros. The
  370.                purpose of these two macros, in this context, is to
  371.                automatically count the number of parameters you are
  372.                pushing. Then whenever Perl is creating the @_ array
  373.                for the sub, it knows how big to make it.
  374.  
  375.                The PUSHMARK macro tells Perl to make a mental note of
  376.                the current stack pointer. Even if you aren't passing
  377.                any parameters (like in Example 1) you must still call
  378.                the PUSHMARK macro before you can call any of the
  379.                _p_e_r_l__c_a_l_l_* functions - Perl still needs to know that
  380.                there are no parameters.
  381.  
  382.                The PUTBACK macro sets the global copy of the stack
  383.                pointer to be the same as our local copy. If we didn't
  384.                do this _p_e_r_l__c_a_l_l__p_v wouldn't know where the two
  385.                parameters we pushed were - remember that up to now all
  386.                the stack pointer manipulation we have done is with our
  387.                local copy, _n_o_t the global copy.
  388.  
  389.  
  390.  
  391.  
  392.  
  393.      Page 6                                          (printed 6/30/95)
  394.  
  395.  
  396.  
  397.  
  398.  
  399.  
  400.      PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
  401.  
  402.  
  403.  
  404.           5.   Next, we come to XPUSHs. This is where the parameters
  405.                actually get pushed onto the stack. In this case we are
  406.                pushing a string and an integer.
  407.  
  408.                See the section _X_S_U_B'_s _A_N_D  _T_H_E _A_R_G_U_M_E_N_T _S_T_A_C_K in the
  409.                _p_e_r_l_g_u_t_s manpage for details on how the XPUSH macros
  410.                work.
  411.  
  412.           6.   Finally, _L_e_f_t_S_t_r_i_n_g can now be called via the
  413.                _p_e_r_l__c_a_l_l__p_v function.
  414.  
  415.           EEEExxxxaaaammmmpppplllleeee 3333:::: RRRReeeettttuuuurrrrnnnniiiinnnngggg aaaa SSSSccccaaaallllaaaarrrr
  416.  
  417.           Now for an example of dealing with the values returned from
  418.           a Perl sub.
  419.  
  420.           Here is a Perl sub, _A_d_d_e_r,  which takes 2 integer parameters
  421.           and simply returns their sum.
  422.  
  423.               sub Adder
  424.               {
  425.                   my($a, $b) = @_ ;
  426.                   $a + $b ;
  427.               }
  428.  
  429.           As we are now concerned with the return value from _A_d_d_e_r,
  430.           the C function is now a bit more complex.
  431.  
  432.               static void
  433.               call_Adder(a, b)
  434.               int a ;
  435.               int b ;
  436.               {
  437.                   dSP ;
  438.                   int count ;
  439.  
  440.                   ENTER ;
  441.                   SAVETMPS;
  442.  
  443.                   PUSHMARK(sp) ;
  444.                   XPUSHs(sv_2mortal(newSViv(a)));
  445.                   XPUSHs(sv_2mortal(newSViv(b)));
  446.                   PUTBACK ;
  447.  
  448.                   count = perl_call_pv("Adder", G_SCALAR);
  449.  
  450.                   SPAGAIN ;
  451.  
  452.                   if (count != 1)
  453.                       croak("Big trouble\n") ;
  454.  
  455.                   printf ("The sum of %d and %d is %d\n", a, b, POPi) ;
  456.  
  457.  
  458.  
  459.      Page 7                                          (printed 6/30/95)
  460.  
  461.  
  462.  
  463.  
  464.  
  465.  
  466.      PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
  467.  
  468.  
  469.  
  470.                   PUTBACK ;
  471.                   FREETMPS ;
  472.                   LEAVE ;
  473.               }
  474.  
  475.           Points to note this time are
  476.  
  477.           1.   The only flag specified this time was G_SCALAR. That
  478.                means the @_ array will be created and that the value
  479.                returned by _A_d_d_e_r will still exist after the call to
  480.                _p_e_r_l__c_a_l_l__p_v.
  481.  
  482.           2.   Because we are interested in what is returned from
  483.                _A_d_d_e_r we cannot specify G_DISCARD. This means that we
  484.                will have to tidy up the Perl stack and dispose of any
  485.                temporary values ourselves. This is the purpose of
  486.  
  487.                        ENTER ;
  488.                        SAVETMPS ;
  489.  
  490.                at the start of the function, and
  491.  
  492.                        FREETMPS ;
  493.                        LEAVE ;
  494.  
  495.                at the end. The ENTER/SAVETMPS pair creates a boundary
  496.                for any temporaries we create. This means that the
  497.                temporaries we get rid of will be limited to those
  498.                which were created after these calls.
  499.  
  500.                The FREETMPS/LEAVE pair will get rid of any values
  501.                returned by the Perl sub, plus it will also dump the
  502.                mortal SV's we created. Having ENTER/SAVETMPS at the
  503.                beginning of the code makes sure that no other mortals
  504.                are destroyed.
  505.  
  506.           3.   The purpose of the macro SPAGAIN is to refresh the
  507.                local copy of the stack pointer. This is necessary
  508.                because it is possible that the memory allocated to the
  509.                Perl stack has been re-allocated whilst in the
  510.                _p_e_r_l__c_a_l_l__p_v call.
  511.  
  512.                If you are making use of the Perl stack pointer in your
  513.                code you must always refresh the your local copy using
  514.                SPAGAIN whenever you make use of of the _p_e_r_l__c_a_l_l_*
  515.                functions or any other Perl internal function.
  516.  
  517.           4.   Although only a single value was expected to be
  518.                returned from _A_d_d_e_r, it is still good practice to check
  519.                the return code from _p_e_r_l__c_a_l_l__p_v anyway.
  520.  
  521.                Expecting a single value is not quite the same as
  522.  
  523.  
  524.  
  525.      Page 8                                          (printed 6/30/95)
  526.  
  527.  
  528.  
  529.  
  530.  
  531.  
  532.      PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
  533.  
  534.  
  535.  
  536.                knowing that there will be one. If someone modified
  537.                _A_d_d_e_r to return a list and we didn't check for that
  538.                possibility and take appropriate action the Perl stack
  539.                would end up in an inconsistant state. That is
  540.                something you _r_e_a_l_l_y don't want to ever happen.
  541.  
  542.           5.   The POPi macro is used here to pop the return value
  543.                from the stack. In this case we wanted an integer, so
  544.                POPi was used.
  545.  
  546.                Here is the complete list of POP macros available,
  547.                along with the types they return.
  548.  
  549.                        POPs    SV
  550.                        POPp    pointer
  551.                        POPn    double
  552.                        POPi    integer
  553.                        POPl    long
  554.  
  555.  
  556.           6.   The final PUTBACK is used to leave the Perl stack in a
  557.                consistant state before exiting the function. This is
  558.                necessary because when we popped the return value from
  559.                the stack with POPi it only updated our local copy of
  560.                the stack pointer. Remember, PUTBACK sets the global
  561.                stack pointer to be the same as our local copy.
  562.  
  563.           EEEExxxxaaaammmmpppplllleeee 4444:::: RRRReeeettttuuuurrrrnnnniiiinnnngggg aaaa lllliiiisssstttt ooooffff vvvvaaaalllluuuueeeessss
  564.  
  565.           Now, let's extend the previous example to return both the
  566.           sum of the parameters and the difference.
  567.  
  568.           Here is the Perl sub
  569.  
  570.               sub AddSubtract
  571.               {
  572.                  my($a, $b) = @_ ;
  573.                  ($a+$b, $a-$b) ;
  574.               }
  575.  
  576.           and this is the C function
  577.  
  578.               static void
  579.               call_AddSubtract(a, b)
  580.               int a ;
  581.               int b ;
  582.               {
  583.                   dSP ;
  584.                   int count ;
  585.  
  586.                   ENTER ;
  587.                   SAVETMPS;
  588.  
  589.  
  590.  
  591.      Page 9                                          (printed 6/30/95)
  592.  
  593.  
  594.  
  595.  
  596.  
  597.  
  598.      PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
  599.  
  600.  
  601.  
  602.                   PUSHMARK(sp) ;
  603.                   XPUSHs(sv_2mortal(newSViv(a)));
  604.                   XPUSHs(sv_2mortal(newSViv(b)));
  605.                   PUTBACK ;
  606.  
  607.                   count = perl_call_pv("AddSubtract", G_ARRAY);
  608.  
  609.                   SPAGAIN ;
  610.  
  611.                   if (count != 2)
  612.                       croak("Big trouble\n") ;
  613.  
  614.                   printf ("%d - %d = %d\n", a, b, POPi) ;
  615.                   printf ("%d + %d = %d\n", a, b, POPi) ;
  616.  
  617.                   PUTBACK ;
  618.                   FREETMPS ;
  619.                   LEAVE ;
  620.               }
  621.  
  622.           Notes
  623.  
  624.           1.   We wanted array context, so we used G_ARRAY.
  625.  
  626.           2.   Not surprisingly there are 2 POPi's this time  because
  627.                we were retrieving 2 values from the stack. The main
  628.                point to note is that they came off the stack in
  629.                reverse order.
  630.  
  631.           EEEExxxxaaaammmmpppplllleeee 5555:::: RRRReeeettttuuuurrrrnnnniiiinnnngggg DDDDaaaattttaaaa ffffrrrroooommmm PPPPeeeerrrrllll vvvviiiiaaaa tttthhhheeee ppppaaaarrrraaaammmmeeeetttteeeerrrr lllliiiisssstttt
  632.  
  633.           It is also possible to return values directly via the
  634.           parameter list - whether it is actually desirable to do it
  635.           is another matter entirely.
  636.  
  637.           The Perl sub, _I_n_c, below takes 2 parameters and increments
  638.           each.
  639.  
  640.               sub Inc
  641.               {
  642.                   ++ $_[0] ;
  643.                   ++ $_[1] ;
  644.               }
  645.  
  646.           and here is a C function to call it.
  647.  
  648.  
  649.  
  650.  
  651.  
  652.  
  653.  
  654.  
  655.  
  656.  
  657.      Page 10                                         (printed 6/30/95)
  658.  
  659.  
  660.  
  661.  
  662.  
  663.  
  664.      PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
  665.  
  666.  
  667.  
  668.               static void
  669.               call_Inc(a, b)
  670.               int a ;
  671.               int b ;
  672.               {
  673.                   dSP ;
  674.                   int count ;
  675.                   SV * sva ;
  676.                   SV * svb ;
  677.  
  678.                   ENTER ;
  679.                   SAVETMPS;
  680.  
  681.                   sva = sv_2mortal(newSViv(a)) ;
  682.                   svb = sv_2mortal(newSViv(b)) ;
  683.  
  684.                   PUSHMARK(sp) ;
  685.                   XPUSHs(sva);
  686.                   XPUSHs(svb);
  687.                   PUTBACK ;
  688.  
  689.                   count = perl_call_pv("Inc", G_DISCARD);
  690.  
  691.                   if (count != 0)
  692.                       croak ("call_Inc : expected 0 return value from 'Inc', got %d\n", count) ;
  693.  
  694.                   printf ("%d + 1 = %d\n", a, SvIV(sva)) ;
  695.                   printf ("%d + 1 = %d\n", b, SvIV(svb)) ;
  696.  
  697.                   FREETMPS ;
  698.                   LEAVE ;
  699.               }
  700.  
  701.           To be able to access the two parameters that were pushed
  702.           onto the stack after they return from _p_e_r_l__c_a_l_l__p_v it is
  703.           necessary to make a note of their addresses - thus the two
  704.           variables sva and svb.
  705.  
  706.           The reason this is necessary is that the area of the Perl
  707.           stack which held them will very likely have been overwritten
  708.           by something else by the time control returns from
  709.           _p_e_r_l__c_a_l_l__p_v.
  710.  
  711.           EEEExxxxaaaammmmpppplllleeee 6666:::: UUUUssssiiiinnnngggg GGGG____EEEEVVVVAAAALLLL
  712.  
  713.           Now an example using G_EVAL. Below is a Perl sub which
  714.           computes the difference of its 2 parameters. If this would
  715.           result in a negative result, the sub calls _d_i_e.
  716.  
  717.               sub Subtract
  718.               {
  719.                   my ($a, $b) = @_ ;
  720.  
  721.  
  722.  
  723.      Page 11                                         (printed 6/30/95)
  724.  
  725.  
  726.  
  727.  
  728.  
  729.  
  730.      PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
  731.  
  732.  
  733.  
  734.                   die "death can be fatal\n" if $a < $b ;
  735.  
  736.                   $a - $b ;
  737.               }
  738.  
  739.           and some C to call it
  740.  
  741.               static void
  742.               call_Subtract(a, b)
  743.               int a ;
  744.               int b ;
  745.               {
  746.                   dSP ;
  747.                   int count ;
  748.                   SV * sv ;
  749.  
  750.                   ENTER ;
  751.                   SAVETMPS;
  752.  
  753.                   PUSHMARK(sp) ;
  754.                   XPUSHs(sv_2mortal(newSViv(a)));
  755.                   XPUSHs(sv_2mortal(newSViv(b)));
  756.                   PUTBACK ;
  757.  
  758.                   count = perl_call_pv("Subtract", G_EVAL|G_SCALAR);
  759.  
  760.                   /* Check the eval first */
  761.                   sv = GvSV(gv_fetchpv("@", TRUE, SVt_PV));
  762.                   if (SvTRUE(sv))
  763.                       printf ("Uh oh - %s\n", SvPV(sv, na)) ;
  764.  
  765.                   SPAGAIN ;
  766.  
  767.                   if (count != 1)
  768.                       croak ("call_Subtract : expected 1 return value from 'Subtract', got %d\n", count) ;
  769.  
  770.                   printf ("%d - %d = %d\n", a, b, POPi) ;
  771.  
  772.                   PUTBACK ;
  773.                   FREETMPS ;
  774.                   LEAVE ;
  775.  
  776.               }
  777.  
  778.           If _c_a_l_l__S_u_b_t_r_a_c_t is called thus
  779.  
  780.                   call_Subtract(4, 5)
  781.  
  782.           the following will be printed
  783.  
  784.                   Uh oh - death can be fatal
  785.  
  786.  
  787.  
  788.  
  789.      Page 12                                         (printed 6/30/95)
  790.  
  791.  
  792.  
  793.  
  794.  
  795.  
  796.      PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
  797.  
  798.  
  799.  
  800.           Notes
  801.  
  802.           1.   We want to be able to catch the _d_i_e so we have used the
  803.                G_EVAL flag.  Not specifying this flag would mean that
  804.                the program would terminate.
  805.  
  806.           2.   The code
  807.  
  808.                        sv = GvSV(gv_fetchpv("@", TRUE, SVt_PV));
  809.                        if (SvTRUE(sv))
  810.                            printf ("Uh oh - %s\n", SvPVx(sv, na)) ;
  811.  
  812.                is the equivalent of this bit of Perl
  813.  
  814.                        print "Uh oh - $@\n" if $@ ;
  815.  
  816.  
  817.           EEEExxxxaaaammmmpppplllleeee 7777:::: UUUUssssiiiinnnngggg ppppeeeerrrrllll____ccccaaaallllllll____ssssvvvv
  818.  
  819.           In all the previous examples I have 'hard-wried' the name of
  820.           the Perl sub to be called from C. Sometimes though, it is
  821.           necessary to be able to specify the name of the Perl sub
  822.           from within the Perl script.
  823.  
  824.           Consider the Perl code below
  825.  
  826.                   sub fred
  827.                   {
  828.                       print "Hello there\n" ;
  829.                   }
  830.  
  831.                   CallSub("fred") ;
  832.  
  833.           here is a snippet of XSUB which defines _C_a_l_l_S_u_b.
  834.  
  835.                   void
  836.                   CallSub(name)
  837.                           char *  name
  838.                           CODE:
  839.                           PUSHMARK(sp) ;
  840.                           perl_call_pv(name, G_DISCARD|G_NOARGS) ;
  841.  
  842.           That is fine as far as it goes. The thing is, it only allows
  843.           the Perl sub to be specified as a string. For perl 4 this
  844.           was adequate, but Perl 5 allows references to subs and
  845.           anonymous subs. This is where _p_e_r_l__c_a_l_l__s_v is useful.
  846.  
  847.           The code below for _C_a_l_l_S_u_b is identical to the previous time
  848.           except that the name parameter is now defined as an SV* and
  849.           we use _p_e_r_l__c_a_l_l__s_v instead of _p_e_r_l__c_a_l_l__p_v.
  850.  
  851.  
  852.  
  853.  
  854.  
  855.      Page 13                                         (printed 6/30/95)
  856.  
  857.  
  858.  
  859.  
  860.  
  861.  
  862.      PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
  863.  
  864.  
  865.  
  866.                   void
  867.                   CallSub(name)
  868.                           SV*     name
  869.                           CODE:
  870.                           PUSHMARK(sp) ;
  871.                           perl_call_sv(name, G_DISCARD|G_NOARGS) ;
  872.  
  873.           As we are using an SV to call _f_r_e_d the following can all be
  874.           used
  875.  
  876.                   CallSub("fred") ;
  877.                   Callsub(\&fred) ;
  878.                   $ref = \&fred ;
  879.                   CallSub($ref) ;
  880.                   CallSub( sub { print "Hello there\n" } ) ;
  881.  
  882.           As you can see, _p_e_r_l__c_a_l_l__s_v gives you greater flexibility
  883.           in how you can specify the Perl sub.
  884.  
  885.           EEEExxxxaaaammmmpppplllleeee 8888:::: UUUUssssiiiinnnngggg ppppeeeerrrrllll____ccccaaaallllllll____aaaarrrrggggvvvv
  886.  
  887.           Here is a Perl sub which prints whatever parameters are
  888.           passed to it.
  889.  
  890.                   sub PrintList
  891.                   {
  892.                       my(@list) = @_ ;
  893.  
  894.                       foreach (@list) { print "$_\n" }
  895.                   }
  896.  
  897.           and here is an example of _p_e_r_l__c_a_l_l__a_r_g_v which will call
  898.           _P_r_i_n_t_L_i_s_t.
  899.  
  900.                   call_PrintList
  901.                   {
  902.                       dSP ;
  903.                       char * words[] = {"alpha", "beta", "gamma", "delta", NULL } ;
  904.  
  905.                       perl_call_argv("PrintList", words, G_DISCARD) ;
  906.                   }
  907.  
  908.           Note that it is not necessary to call PUSHMARK in this
  909.           instance. This is because _p_e_r_l__c_a_l_l__a_r_g_v will do it for you.
  910.  
  911.           EEEExxxxaaaammmmpppplllleeee 9999:::: UUUUssssiiiinnnngggg ppppeeeerrrrllll____ccccaaaallllllll____mmmmeeeetttthhhhoooodddd
  912.  
  913.           [This section is under construction]
  914.  
  915.           Consider the following Perl code
  916.  
  917.  
  918.  
  919.  
  920.  
  921.      Page 14                                         (printed 6/30/95)
  922.  
  923.  
  924.  
  925.  
  926.  
  927.  
  928.      PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
  929.  
  930.  
  931.  
  932.                   {
  933.                     package Mine ;
  934.  
  935.                     sub new     { bless [@_] }
  936.                     sub Display { print $_[0][1], "\n" }
  937.                   }
  938.  
  939.                   $a = new Mine ('red', 'green', 'blue') ;
  940.                   call_Display($a, 'Display') ;
  941.  
  942.           The method Display just prints out the first element of the
  943.           list.  Here is a XSUB implementation of _c_a_l_l__D_i_s_p_l_a_y.
  944.  
  945.                   void
  946.                   call_Display(ref, method)
  947.                       SV *    ref
  948.                       char *  method
  949.                       CODE:
  950.                       PUSHMARK(sp);
  951.                       XPUSHs(ref);
  952.                       PUTBACK;
  953.  
  954.                       perl_call_method(method, G_DISCARD) ;
  955.  
  956.  
  957.           SSSSttttrrrraaaatttteeeeggggiiiieeeessss ffffoooorrrr ssssttttoooorrrriiiinnnngggg CCCCoooonnnntttteeeexxxxtttt IIIInnnnffffoooorrrrmmmmaaaattttiiiioooonnnn
  958.  
  959.           [This section is under construction]
  960.  
  961.           One of the trickiest problems to overcome when designing a
  962.           callback interface is figuring out how to store the mapping
  963.           between the C callback functions and the Perl equivalent.
  964.  
  965.           Consider the following example.
  966.  
  967.           AAAAlllltttteeeerrrrnnnnaaaatttteeee SSSSttttaaaacccckkkk MMMMaaaannnniiiippppuuuullllaaaattttiiiioooonnnn
  968.  
  969.           [This section is under construction]
  970.  
  971.           Although I have only made use of the POP* macros to access
  972.           values returned from Perl subs, it is also possible to
  973.           bypass these macros and read the stack directly.
  974.  
  975.           The code below is example 4 recoded to
  976.  
  977.      SSSSEEEEEEEE AAAALLLLSSSSOOOO
  978.           the _p_e_r_l_a_p_i manpage, the _p_e_r_l_g_u_t_s manpage, the _p_e_r_l_e_m_b_e_d
  979.           manpage
  980.  
  981.      AAAAUUUUTTTTHHHHOOOORRRR
  982.           Paul Marquess <pmarquess@bfsec.bt.co.uk>
  983.  
  984.  
  985.  
  986.  
  987.      Page 15                                         (printed 6/30/95)
  988.  
  989.  
  990.  
  991.  
  992.  
  993.  
  994.      PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111)))) UUUUNNNNIIIIXXXX SSSSyyyysssstttteeeemmmm VVVV ((((RRRReeeelllleeeeaaaasssseeee 0000....0000 PPPPaaaattttcccchhhhlllleeeevvvveeeellll 00000000)))) PPPPEEEERRRRLLLLCCCCAAAALLLLLLLL((((1111))))
  995.  
  996.  
  997.  
  998.           Special thanks to the following people who assisted in the
  999.           creation of the document.
  1000.  
  1001.           Jeff Okamoto, Tim Bunce.
  1002.  
  1003.      DDDDAAAATTTTEEEE
  1004.           Version 0.4, 17th October 1994
  1005.  
  1006.  
  1007.  
  1008.  
  1009.  
  1010.  
  1011.  
  1012.  
  1013.  
  1014.  
  1015.  
  1016.  
  1017.  
  1018.  
  1019.  
  1020.  
  1021.  
  1022.  
  1023.  
  1024.  
  1025.  
  1026.  
  1027.  
  1028.  
  1029.  
  1030.  
  1031.  
  1032.  
  1033.  
  1034.  
  1035.  
  1036.  
  1037.  
  1038.  
  1039.  
  1040.  
  1041.  
  1042.  
  1043.  
  1044.  
  1045.  
  1046.  
  1047.  
  1048.  
  1049.  
  1050.  
  1051.  
  1052.  
  1053.      Page 16                                         (printed 6/30/95)
  1054.  
  1055.  
  1056.  
  1057.